﻿using System.Collections.ObjectModel;
using System.Threading.Tasks;
using LiveHub.DataModels;

namespace LiveHub.AvaloniaApp.ViewModels.Pages;

public class ChatViewModel : PageModelBase
{

    public ChatViewModel(string jwt, UserModel user, GroupModel group)
    {
        Jwt = jwt;
        User = user;
        Group = group;
    }
    
    public ObservableCollection<MessageModel> Messages { get; set; } = [];
    private GroupModel _group = new();

    public GroupModel Group
    {
        get => _group;
        set
        {
            _group = value;
            OnPropertyChanged();
        }
    }

    private UserModel _user = new();

    public UserModel User
    {
        get => _user;
        set
        {
            _user = value;
            OnPropertyChanged();
        }
    }

    private string Jwt { get; set; }

    public async Task ToChatGroup()
    {
        using var http = new UserHttp();
        http.Jwt = Jwt;
        var g = await http.GetGroup(Group.GroupId);
        if (g == null) return;
        Group = g;
        Messages.Clear();
        foreach (var message in g.Messages) Messages.Add(message);
    }
}